import { getInputs } from '../src/input-check';
import {expect} from 'chai'
import { env } from 'process';
import {restore} from 'sinon'

describe('getInputs', () => {
    let originalEnv: NodeJS.ProcessEnv;

    beforeEach(() => {
        originalEnv = { ...env };
        env['WORK_SPACE']='sss'
        env['PROJECT_DIR']='sss'
        env['uploadType']='sss'
        env['ARCHIVE_PATH']='sss'
        env['ARTIFACT']='sss'
        env['ARTIFACT_NAME']='sss'
        env['ARTIFACT']='sss'
        env['DATETIME']='sss'
        env['stepIdentifier']='sss'
        env['ARTIFACT_PATH']='sss'
        env['FILE_PARENT_ID']='sss'
        env['ENGINE_GLOBAL_PARAM_ORGANIZATION_ID']='sss'
        env['ARTIFACT_VERSION']='sss'

    });

    afterEach(() => {
        restore();
        Object.assign(env, originalEnv);
    });

    it('should throw an error if WORK_SPACE is not defined', async () => {
        try {
            delete process.env['WORK_SPACE'];
            await getInputs()
            expect.fail('Expected an error to be thrown');
        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'WORK_SPACE'");

        }
    });

    it('should throw an error if PROJECT_DIR is not defined', async () => {
        try {
            delete process.env['PROJECT_DIR'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'PROJECT_DIR'");

        }
    });

    it('should throw an error if uploadType is not defined', async () => {
        try {
            delete process.env['uploadType'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'uploadType'");

        }
    });

    it('should throw an error if ARTIFACT is not defined', async () => {
        try {
            delete process.env['ARTIFACT'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'ARTIFACT'");

        }
    });

    it('should throw an error if ARTIFACT contains spaces', async () => {
        try {
            process.env['ARTIFACT'] = 'artifact name with spaces';
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("invalid artifact name");

        }
    });

    it('should throw an error if ARTIFACT_NAME is not defined', async () => {
        try {
            delete process.env['ARTIFACT_NAME'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'ARTIFACT_NAME'");

        }
    });

    it('should throw an error if ARTIFACT_PATH is not defined', async () => {
        try {
            delete process.env['ARTIFACT_PATH'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'ARTIFACT_NAME'");

        }
    });

    it('should throw an error if DATETIME is not defined', async () => {
        try {
            delete process.env['DATETIME'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'DATETIME'");

        }
    });

    it('should throw an error if ARTIFACT_VERSION has space', async () => {
        try {
            process.env['ARTIFACT_VERSION'] = ' ';
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            console.log(error)
            // @ts-ignore
            expect(error.message).to.contain("invalid artifact version");

        }
    });

    it('should throw an error if stepIdentifier is not defined', async () => {
        try {
            delete process.env['stepIdentifier'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'stepIdentifier'");

        }
    });

    it('should throw an error if FILE_PARENT_ID is not defined', async () => {
        try {
            delete process.env['FILE_PARENT_ID'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'FILE_PARENT_ID'");

        }
    });

    it('should throw an error if ENGINE_GLOBAL_PARAM_ORGANIZATION_ID is not defined', async () => {
        try {
            delete process.env['ENGINE_GLOBAL_PARAM_ORGANIZATION_ID'];
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain("missing params  'ENGINE_GLOBAL_PARAM_ORGANIZATION_ID'");

        }
    });

    it('should return default values for optional parameters', async () => {
        delete process.env['REPO_ID'] ;
        delete process.env['GIT_IGNORE'] ;

        const inputs = await getInputs();

        expect(inputs.repoId).equal('flow_generic_repo');
        expect(inputs.gitIgnore).equal('false');
    });

    it('should return correct UploadInputs object when all required parameters are defined', async () => {
        process.env['WORK_SPACE'] = 'workspace';
        process.env['PROJECT_DIR'] = 'projectDir';
        process.env['uploadType'] = 'uploadType';
        process.env['ARTIFACT'] = 'artifact';
        process.env['ARTIFACT_NAME'] = 'artifactName';
        process.env['ARTIFACT_PATH'] = 'artifactPath';
        process.env['DATETIME'] = '2023-10-01T10:00:00Z';
        process.env['stepIdentifier'] = 'stepIdentifier';
        process.env['FILE_PARENT_ID'] = 'fileParentId';
        process.env['ENGINE_GLOBAL_PARAM_ORGANIZATION_ID'] = 'orgId';
        delete process.env['ARTIFACT_VERSION'];
        const inputs = await getInputs();

        expect(inputs.workDir).equal('workspace');
        expect(inputs.projectDir).equal('projectDir');
        expect(inputs.uploadType).equal('uploadType');
        expect(inputs.artifact).equal('artifact');
        expect(inputs.artifactName).equal('artifactName');
        expect(inputs.artifactPath).equal('artifactPath');
        expect(inputs.dateTime).equal('2023-10-01T10:00:00Z');
        expect(inputs.stepIdentifier).equal('stepIdentifier');
        expect(inputs.fileParentId).equal('fileParentId');
        expect(inputs.orgId).equal('orgId');
    });
});
